import type { App } from "vue";
import { createRouter, createWebHashHistory, type RouteRecordRaw } from "vue-router";

export const Layout = () => import("@/layout/index.vue");

// 静态路由
export const constantRoutes: RouteRecordRaw[] = [
  {
    path: "/redirect",
    component: Layout,
    meta: { hidden: true },
    children: [
      {
        path: "/redirect/:path(.*)",
        component: () => import("@/views/redirect/index.vue"),
      },
    ],
  },
  {
    path: "/apply",
    component: () => import("@/views/apply/index.vue"),
    meta: { hidden: true, notNeedLogin: true },
  },
  {
    path: "/login",
    component: () => import("@/views/login/index.vue"),
    meta: { hidden: true, notNeedLogin: true },
  },
  {
    path: "/screen/statistics",
    component: () => import("@/views/screen/statistics.vue"),
    meta: { hidden: true },
  },
  {
    path: "/",
    name: "/",
    component: Layout,
    redirect: "/dashboard",
    meta: { roles: ["superuser", "staffer", "supplier", "agenter"], title: "统计" },
    children: [
      {
        path: "dashboard",
        component: () => import("@/views/dashboard/index.vue"),
        // 用于 keep-alive 功能，需要与 SFC 中自动推导或显式声明的组件名称一致
        // 参考文档: https://cn.vuejs.org/guide/built-ins/keep-alive.html#include-exclude
        name: "Dashboard",
        meta: {
          title: "供应商统计",
          icon: "homepage",
          affix: true,
          keepAlive: true,
          roles: ["superuser", "staffer", "supplier"],
        },
      },
      {
        path: "/dashboard/agent",
        component: () => import("@/views/dashboard/agent/index.vue"),
        name: "DashboardAgent",
        meta: {
          title: "代理商统计",
          icon: "el-icon-data-analysis",
          roles: ["agenter"],
        },
      },
      {
        path: "401",
        component: () => import("@/views/error/401.vue"),
        meta: { hidden: true },
      },
      {
        path: "404",
        component: () => import("@/views/error/404.vue"),
        meta: { hidden: true },
      },
      {
        path: "profile",
        name: "Profile",
        component: () => import("@/views/profile/index.vue"),
        meta: { title: "个人中心", icon: "user", hidden: true, roles: ["superuser", "staffer"] },
      },
      {
        path: "myNotice",
        name: "MyNotice",
        component: () => import("@/views/system/notice/components/MyNotice.vue"),
        meta: { title: "我的通知", icon: "user", hidden: true },
      },
    ],
  },
  {
    path: "/task",
    name: "Task",
    component: Layout,
    meta: {
      title: "任务管理",
      icon: "el-icon-Operation",
      roles: ["superuser", "staffer"],
      alwaysShow: true,
    },
    children: [
      {
        path: "task",
        name: "TaskList",
        component: () => import("@/views/task/task/index.vue"),
        meta: {
          title: "任务列表",
          icon: "el-icon-List",
          roles: ["superuser", "staffer"],
        },
      },
      {
        path: "log",
        name: "TaskLog",
        component: () => import("@/views/task/log/index.vue"),
        meta: {
          title: "任务日志",
          icon: "el-icon-Document",
          roles: ["superuser", "staffer"],
        },
      },
    ],
  },
  {
    path: "/goods",
    name: "/goods",
    component: Layout,
    meta: {
      title: "商品管理",
      icon: "el-icon-Goods",
      roles: ["superuser", "supplier"],
    },
    children: [
      {
        path: "goods",
        component: () => import("@/views/goods/index.vue"),
        name: "goods",
        meta: {
          title: "商品列表",
          icon: "el-icon-Goods",
          roles: ["superuser", "supplier"],
        },
      },
      {
        path: "add",
        component: () => import("@/views/goods/add.vue"),
        name: "add",
        meta: {
          title: "发布商品",
          hidden: true,
          roles: ["superuser", "supplier"],
        },
      },
      {
        path: "category",
        component: () => import("@/views/goods/category.vue"),
        name: "category",
        meta: {
          title: "商品分类",
          icon: "el-icon-menu",
          roles: ["staffer"],
        },
      },
      {
        path: "uom",
        component: () => import("@/views/goods/uom.vue"),
        name: "uom",
        meta: {
          title: "商品单位",
          icon: "el-icon-discount",
          roles: ["staffer"],
        },
      },
      {
        path: "activity",
        component: () => import("@/views/goods/activity.vue"),
        name: "activity",
        meta: {
          title: "促销产品",
          icon: "el-icon-price-tag",
          roles: ["supplier"],
        },
      },
      {
        path: "shipping-template",
        component: () => import("@/views/goods/shipping-template/index.vue"),
        name: "shipping-template",
        meta: {
          title: "运费模板",
          icon: "el-icon-box",
          roles: ["supplier"],
        },
      },
      {
        path: "brand",
        component: () => import("@/views/goods/brand.vue"),
        name: "brand",
        meta: {
          title: "商品品牌",
          icon: "el-icon-suitcase",
          roles: ["supplier"],
        },
      },
    ],
  },
  {
    path: "/coupon",
    name: "Coupon",
    component: Layout,
    meta: {
      title: "优惠券管理",
      icon: "el-icon-Present",
      roles: ["superuser", "staffer"],
      alwaysShow: true,
    },
    children: [
      {
        path: "coupon",
        name: "CouponList",
        component: () => import("@/views/coupon/coupon/index.vue"),
        meta: {
          title: "优惠券列表",
          icon: "el-icon-List",
          roles: ["superuser", "staffer"],
        },
      },
      {
        path: "receive",
        name: "CouponReceive",
        component: () => import("@/views/coupon/receive/index.vue"),
        meta: {
          title: "领取记录",
          icon: "el-icon-List",
          roles: ["superuser", "staffer"],
        },
      },
    ],
  },
  {
    path: "/order",
    name: "/order",
    component: Layout,
    meta: {
      title: "订单管理",
      icon: "el-icon-Document",
      roles: ["supplier", "staffer"],
    },
    children: [
      {
        path: "order",
        component: () => import("@/views/order/index.vue"),
        name: "order",
        meta: {
          title: "订单列表",
          roles: ["supplier", "staffer"],
        },
      },
      {
        path: "afterSales",
        component: () => import("@/views/order/afterSales.vue"),
        name: "afterSales",
        meta: {
          title: "售后列表",
          roles: ["supplier", "staffer"],
        },
      },
    ],
  },
  {
    path: "/logistics",
    name: "Logistics",
    component: Layout,
    meta: {
      title: "物流管理",
      icon: "el-icon-Van",
      roles: ["supplier", "staffer", "superuser"],
      alwaysShow: true,
    },
    children: [
      {
        path: "company",
        name: "ExpressCompany",
        component: () => import("@/views/logistics/company/index.vue"),
        meta: {
          title: "快递公司",
          icon: "el-icon-OfficeBuilding",
          roles: ["staffer", "superuser"],
        },
      },
      {
        path: "express",
        name: "Express",
        component: () => import("@/views/logistics/express/index.vue"),
        meta: {
          title: "快递单管理",
          icon: "el-icon-ScaleToOriginal",
          roles: ["staffer", "superuser"],
        },
      },
    ],
  },
  // {
  //   path: "/member",
  //   name: "/member",
  //   component: Layout,
  //   children: [
  //     {
  //       path: "member",
  //       component: () => import("@/views/member/index.vue"),
  //       name: "member",
  //       meta: {
  //         title: "会员管理",
  //         icon: "el-icon-User"
  //       },
  //     }
  //   ]
  // },
  {
    path: "/finance",
    name: "/finance",
    redirect: "/finance/finance",
    component: Layout,
    meta: {
      title: "财务管理",
      icon: "el-icon-Wallet",
      roles: ["supplier", "agenter"],
    },
    children: [
      {
        path: "finance",
        component: () => import("@/views/finance/index.vue"),
        name: "finance",
        meta: {
          title: "收入明细",
          roles: ["supplier"],
        },
      },
      {
        path: "withdrawal",
        component: () => import("@/views/finance/withdrawal.vue"),
        name: "withdrawal",
        meta: {
          title: "余额提现",
          roles: ["supplier", "agenter"],
        },
      },
      {
        path: "commission",
        component: () => import("@/views/finance/commission/index.vue"),
        name: "Commission",
        meta: {
          title: "提成明细",
          roles: ["agenter"],
        },
      },
    ],
  },
  {
    path: "/payment",
    name: "Payment",
    component: Layout,
    meta: {
      title: "支付管理",
      icon: "el-icon-Money",
      roles: ["superuser", "staffer"],
      alwaysShow: true,
    },
    children: [
      {
        path: "payment",
        name: "PaymentList",
        component: () => import("@/views/payment/payment/index.vue"),
        meta: {
          title: "支付订单",
          icon: "el-icon-List",
          roles: ["superuser", "staffer"],
        },
      },
      {
        path: "refund",
        name: "PaymentRefund",
        component: () => import("@/views/payment/refund/index.vue"),
        meta: {
          title: "支付退款单",
          icon: "el-icon-List",
          roles: ["superuser", "staffer"],
        },
      },
    ],
  },
  {
    path: "/merchant",
    name: "Merchant",
    redirect: "/merchant/merchant",
    component: Layout,
    meta: {
      title: "商户管理",
      icon: "el-icon-suitcase",
      roles: ["staffer", "agenter"],
      alwaysShow: true,
    },
    children: [
      {
        path: "merchant",
        name: "MerchantList",
        component: () => import("@/views/merchant/merchant/index.vue"),
        meta: {
          title: "商户列表",
          icon: "el-icon-suitcase",
          roles: ["staffer"],
        },
      },
      {
        path: "agent",
        name: "MerchantAgent",
        component: () => import("@/views/merchant/agent/index.vue"),
        meta: {
          title: "属下商户",
          icon: "el-icon-grid",
          roles: ["agenter"],
        },
      },
      {
        path: "transactions",
        name: "MerchantTransactions",
        component: () => import("@/views/merchant/transactions/index.vue"),
        meta: {
          title: "交易明细",
          icon: "el-icon-grid",
          roles: ["agenter"],
          hidden: true,
        },
      },
      {
        path: "subject",
        name: "MerchantSubject",
        component: () => import("@/views/merchant/subject/index.vue"),
        meta: {
          title: "商户类目",
          icon: "el-icon-grid",
          roles: ["staffer"],
        },
      },
    ],
  },
  {
    path: "/user",
    name: "User",
    redirect: "/user/profile",
    component: Layout,
    meta: {
      title: "用户管理",
      icon: "el-icon-user",
      roles: ["superuser", "staffer"],
      alwaysShow: true,
    },
    children: [
      {
        path: "profile",
        name: "UserProfile",
        component: () => import("@/views/user/profile/index.vue"),
        meta: {
          title: "用户列表",
          icon: "el-icon-list",
          roles: ["superuser", "staffer"],
        },
      },
      {
        path: "recharge",
        name: "Recharge",
        component: () => import("@/views/user/recharge/index.vue"),
        meta: {
          title: "充值订单",
          icon: "el-icon-document",
          roles: ["superuser", "staffer"],
        },
      },
      {
        path: "account",
        name: "Account",
        component: () => import("@/views/user/account/index.vue"),
        meta: {
          title: "账单明细",
          icon: "el-icon-document",
          roles: ["superuser", "staffer"],
        },
      },
    ],
  },
  {
    path: "/supplier",
    name: "Supplier",
    component: Layout,
    meta: {
      title: "供应商管理",
      icon: "el-icon-Coin",
      alwaysShow: true,
      roles: ["superuser", "staffer", "agenter"],
    },
    children: [
      {
        path: "supplier",
        component: () => import("@/views/supplier/supplier/index.vue"),
        name: "SupplierManage",
        meta: {
          title: "供应商列表",
          icon: "el-icon-user",
          roles: ["staffer"],
        },
      },
      {
        path: "agent",
        component: () => import("@/views/supplier/agent/index.vue"),
        name: "SupplierAgent",
        meta: {
          title: "属下供应商",
          icon: "el-icon-user",
          roles: ["agenter"],
        },
      },
      {
        path: "transactions",
        component: () => import("@/views/supplier/transactions/index.vue"),
        name: "SupplierTransactions",
        meta: {
          title: "交易流水",
          icon: "el-icon-user",
          roles: ["agenter"],
          hidden: true,
        },
      },
      {
        path: "apply",
        component: () => import("@/views/supplier/apply/index.vue"),
        name: "SupplierApply",
        meta: {
          title: "供应商申请",
          icon: "el-icon-user",
          roles: ["staffer"],
        },
      },
      {
        path: "account",
        component: () => import("@/views/supplier/account/index.vue"),
        name: "SupplierAccount",
        meta: {
          title: "供应商账单",
          icon: "el-icon-user",
          roles: ["staffer"],
        },
      },
    ],
  },
  {
    path: "/forum/invitation",
    name: "ForumInvitation",
    component: () => import("@/views/forum/invitation.vue"),
  },
  {
    path: "/forum",
    name: "Forum",
    component: Layout,
    meta: {
      title: "全球食品饮料论坛",
      icon: "el-icon-ChatDotRound",
      roles: ["staffer", "superuser"],
      alwaysShow: true,
    },
    children: [
      {
        path: "list",
        component: () => import("@/views/forum/list.vue"),
        name: "ForumList",
        meta: {
          title: "列表",
          icon: "el-icon-user",
          roles: ["staffer", "superuser"],
        },
      },
    ],
  },
  {
    path: "/agent",
    name: "Agent",
    component: Layout,
    meta: {
      title: "代理商管理",
      icon: "el-icon-Coin",
      alwaysShow: true,
      roles: ["staffer", "superuser"],
    },
    children: [
      {
        path: "agent",
        component: () => import("@/views/agent/agent/index.vue"),
        name: "AgentManage",
        meta: {
          title: "代理商列表",
          icon: "el-icon-user",
          roles: ["staffer", "superuser"],
        },
      },
      {
        path: "examine",
        component: () => import("@/views/agent/examine/index.vue"),
        name: "AgentExamine",
        meta: {
          title: "切换区域代理申请",
          icon: "el-icon-user",
          roles: ["staffer", "superuser"],
        },
      },
      {
        path: "regional-agent",
        name: "RegionalAgent",
        component: () => import("@/views/user/regional-agent/index.vue"),
        meta: {
          title: "区域代理",
          icon: "el-icon-document",
          roles: ["superuser", "staffer"],
        },
      },
    ],
  },
  {
    path: "/system",
    name: "/system",
    redirect: "/system/banner",
    component: Layout,
    meta: {
      title: "系统管理",
      icon: "el-icon-setting",
      roles: ["superuser"],
      alwaysShow: true,
    },
    children: [
      {
        path: "banner",
        component: () => import("@/views/system/banner/index.vue"),
        name: "banner",
        meta: {
          title: "BANNER",
          icon: "el-icon-picture",
          roles: ["superuser"],
        },
      },
      {
        path: "field",
        component: () => import("@/views/system/field/index.vue"),
        name: "field",
        meta: {
          title: "配置字段",
          icon: "el-icon-tickets",
          roles: ["superuser"],
        },
      },
      {
        path: "third-app",
        component: () => import("@/views/system/third-app/index.vue"),
        name: "third-app",
        meta: {
          title: "第三方应用",
          icon: "el-icon-tickets",
          roles: ["superuser"],
        },
      },
      {
        path: "api-key",
        component: () => import("@/views/system/api-key/index.vue"),
        name: "api-key",
        meta: {
          title: "API KEY",
          icon: "el-icon-tickets",
          roles: ["superuser"],
        },
      },
    ],
  },
];

/**
 * 创建路由
 */
const router = createRouter({
  history: createWebHashHistory(),
  routes: constantRoutes,
  // 刷新时，滚动条位置还原
  scrollBehavior: () => ({ left: 0, top: 0 }),
});

// 全局注册 router
export function setupRouter(app: App<Element>) {
  app.use(router);
}

export default router;
